// PicEditor.cpp : implementation file
//

#include "stdafx.h"
#include "../PatchCluster.h"
#include "PicEditor.h"


// CPicEditor

IMPLEMENT_DYNAMIC(CPicEditor, CStatic)

CPicEditor::CPicEditor()
: m_rcHotRegion(0, 0, 0, 0)
{
}

CPicEditor::~CPicEditor()
{
}


BEGIN_MESSAGE_MAP(CPicEditor, CStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


void CPicEditor::GetHotRegion(CRect& rc)
{
	rc.CopyRect(m_rcHotRegion);
}

void CPicEditor::ClearHotRegion()
{
	m_rcHotRegion.SetRect(0, 0, 0, 0);
	Invalidate();
}

// CPicEditor message handlers

void CPicEditor::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	RECT rcClient;
	::GetClientRect(GetSafeHwnd(), &rcClient);

	dc.FillSolidRect(&rcClient, RGB(255, 255, 255));
	HBITMAP hBmp = this->GetBitmap();
	if(hBmp)
	{
		BITMAP bmp;
		HDC hDCSource;
		HBITMAP hBmpOld;
		::GetObject(hBmp, sizeof(BITMAP), &bmp);
		hDCSource = ::CreateCompatibleDC(dc.GetSafeHdc());
		hBmpOld = (HBITMAP)::SelectObject(hDCSource, hBmp);

		::BitBlt(dc.GetSafeHdc(), 0, 0, bmp.bmWidth, bmp.bmHeight, hDCSource, 0, 0, SRCCOPY);

		::SelectObject(hDCSource, hBmpOld);
		::DeleteDC(hDCSource);

		dc.Draw3dRect(&m_rcHotRegion, RGB(255, 0, 0), RGB(255, 0, 0));
	}
}

BOOL CPicEditor::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CPicEditor::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_ptMouseDown = point;
	CStatic::OnLButtonDown(nFlags, point);
}

void CPicEditor::OnMouseMove(UINT nFlags, CPoint point)
{
	if((nFlags & MK_LBUTTON) > 0)
	{
		m_rcHotRegion.left = min(point.x, m_ptMouseDown.x);
		m_rcHotRegion.right = max(point.x, m_ptMouseDown.x);
		m_rcHotRegion.top = min(point.y, m_ptMouseDown.y);
		m_rcHotRegion.bottom= max(point.y, m_ptMouseDown.y);
		Invalidate();
	}
	CStatic::OnMouseMove(nFlags, point);
}
